package simulation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;

import maze.Location;
import maze.Maze;
import robots.OwnGhost;
import robots.Pacman;
import robots.Robot;
import robots.VirtualGhost;

public class Simulator {
	private Maze maze;
	private ColorMap colormap;
	private ArrayList<OwnGhost> ghosts = new ArrayList<OwnGhost>();
	private Map<OwnGhost, Location> ghostLocations;
	private Map<OwnGhost, Integer> ghostRotations; // degrees
	private Map<OwnGhost, Semaphore> ghostLocks;

	public Map<OwnGhost, Semaphore> getGhostLocks() {
		return ghostLocks;
	}

	private Pacman pacman;
	private Location pacmanLocation;
	private int pacmanRotation;
	private PacmanPilot pacmanPilot;

	

	public Simulator(Maze m, ColorMap c) {
		maze = m;
		colormap = c;
		ghostLocations = new HashMap<OwnGhost, Location>();
		ghostRotations = new HashMap<OwnGhost, Integer>();
		ghostLocks = new HashMap<OwnGhost, Semaphore>();
		pacmanPilot = new PacmanPilot(this);
	}

	public Maze getMaze() {
		return maze;
	}
	public void setMaze(Maze maze){
		this.maze = maze;
	}

	public ColorMap getColorMap() {
		return colormap;
	}

	public Map<OwnGhost, Location> getGhostLocations() {
		return ghostLocations;
	}

	public Map<OwnGhost, Integer> getGhostRotations() {
		return ghostRotations;
	}

	public Pacman getPacman() {
		return pacman;
	}

	public void setPacman(Pacman pacman) {
		this.pacman = pacman;
		setPacmanLocation(pacman.getLocation());
	}

	public void addGhost(OwnGhost ghost, Location loc, int rotation) {
		ghosts.add(ghost);
		ghostLocations.put(ghost, loc);
		ghostRotations.put(ghost, rotation);
		ghostLocks.put(ghost, new Semaphore(1));
		try {
			ghostLocks.get(ghost).acquire();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public Location getPacmanLocation() {
		return pacman.getLocation();
	}

	/**
	 * also sets the new location for pacman himself so he can be used in game.
	 * @param pacmanLocation
	 */
	public void setPacmanLocation(Location pacmanLocation) {
		pacman.setLocation(pacmanLocation);
		this.pacmanLocation = pacmanLocation;
		
	}

	public int getPacmanRotation() {
		return pacmanRotation;
	}

	public void setPacmanRotation(int pacmanRotation) {
		this.pacmanRotation = pacmanRotation;
	}

	public PacmanPilot getPacmanPilot() {
		return pacmanPilot;
	}

	
	
}
